package com.scpii.universal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.PushMessageTab;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.SQLs;

public class PushMessageTabService implements BaseInterface<PushMessageTab> {
	private static final Logger log = LoggerFactory
			.getLogger(PushMessageTabService.class);
//	private BaseDao<PushMessageTab> pushMessageTabDao = SpringBeanFactory.pushMessageTabDao;
	private BaseDao<PushMessageTab> pushMessageTabDao;
	
	public PushMessageTab get(Integer id) throws Exception {
		return pushMessageTabDao.get(id);
	}

	public List<PushMessageTab> getAll() throws Exception {
		return pushMessageTabDao.getAll();
	}

	public PushMessageTab get(String propertyName, Object value) throws Exception {
		return null;
	}

	public List<PushMessageTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(PushMessageTab obj) throws Exception {
		pushMessageTabDao.save(obj);
	}

	public void update(PushMessageTab obj) throws Exception {

	}

	public void delete(PushMessageTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<PushMessageTab> hqlGetList(String hql, Map<String, Object> argsMap)
			throws Exception {
		return pushMessageTabDao.hqlGetList(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		pushMessageTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return pushMessageTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<PushMessageTab> sqlGetList(String sqls,
			Map<String, Object> argsMap) throws Exception {
		return pushMessageTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		pushMessageTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return pushMessageTabDao.sqlGetNum(sqls, argsMap);
	}
	
	/**
	 * 根据应用Id，获得推送消息的列表
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午11:25:18
	 */
	public List<PushMessageTab> getPushMessageListByAppId(int appId)throws Exception{
		if (appId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			
			List<PushMessageTab> pushMsgList = this.sqlGetList(SQLs.GET_PUSH_MSG_APPID, argsMap);
			
			return pushMsgList;
		}
		return null;
	}
	
	/**
	 * 获取推送消息的页数
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午11:27:54
	 */
	public Integer getPushMessagePageCount(int appId)throws Exception{
		if (appId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			
			Long resultCount = this.hqlGetNums("", argsMap);
			int result = resultCount.intValue();
			int pageCount = pushMessageTabDao.getRecordPageNum(result, Const.MOBILE_CLIENT_NO_PERPAGE);
			
			return pageCount;
		}
		return 0;
	}
	
	/**
	 * 分页获取消息推送列表
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午11:29:58
	 */
	public List<PushMessageTab> getPushMessageListByPageNum(int appId,int pageNum)throws Exception{
		if (appId>0) {
			List<PushMessageTab> pushMessageList = new ArrayList<PushMessageTab>();
			
			int MAXROWNUM = pageNum * Const.MOBILE_CLIENT_NO_PERPAGE;
			int MINROWNUM = (pageNum-1) * Const.MOBILE_CLIENT_NO_PERPAGE;
			
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			argsMap.put("MAXROWNUM", MAXROWNUM);
			argsMap.put("MINROWNUM", MINROWNUM);
			
			pushMessageList = this.sqlGetList(SQLs.GET_PUSH_MESSAGE_LIST_BY_PAGE, argsMap);
			
			return pushMessageList;
		}
		return null;
	}

	//Setter
	
	public void setPushMessageTabDao(BaseDao<PushMessageTab> pushMessageTabDao) {
		this.pushMessageTabDao = pushMessageTabDao;
	}
	
	public static void main(String[] args) {
		PushMessageTabService pushMsgService = new PushMessageTabService();
		try {
			List<PushMessageTab> pushMsgList = pushMsgService.getPushMessageListByAppId(20120711);
			System.out.println("查询结果："+pushMsgList.size());
			for (int i = 0; i < pushMsgList.size(); i++) {
				System.out.println(pushMsgList.get(i).getVarPushMsgContent());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}